package hikari.managers;

import com.gooddies.wiring.Wiring;
import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.gui.MainWindow;
import hikari.gui.assets.AssetsTree;
import hikari.gui.panels.ModifyPanel;
import hikari.gui.panels.ScriptPanel;
import hikari.objects3d.Basic3dObject;
import hikari.utils.TemplateEngine;
import hikari.utils.Utils;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;

/**
 * @author sad
 */
@WiringComponent(singleton = true, lazy = true)
public class ScriptManager {
    public static final String SCRIPT_EXTENSION = "cpp";
    public static final String SCRIPT_HEADER_EXTENSION = "h";
    @Wire
    private AssetsTree assetsTree;
    @Wire
    private SelectionManager selectionManager;
    @Wire
    private ModifyPanel modifyPanel;
    @Wire
    private MainWindow mainWindow;
    @Wire
    private Utils utils;

    private ScriptManager() {
    }

    public boolean createNewScript(String folder, String fileName) {
        String hFileName = fileName;
        if (!hFileName.toLowerCase().endsWith("." + SCRIPT_HEADER_EXTENSION)) {
            hFileName = hFileName + "." + SCRIPT_HEADER_EXTENSION;
        }

        File headerFile = new File(utils.combinePaths(folder, hFileName));
        if (headerFile.exists()) {
            assetsTree.errorTooltip("Folder already contains file with name " + hFileName);
            return false;
        }

        createHeaderFile(folder, hFileName);
        return true;
    }

    private void createHeaderFile(String folder, String fileName) {
        try {
            String templateFilePath = utils.combinePaths(utils.getWorkDirectory(), "templates");
            templateFilePath = utils.combinePaths(templateFilePath, "hTemplate.h");
            String value = FileUtils.readFileToString(new File(templateFilePath));
            String fNameWithoutExtension = utils.getFileNameWithoutExtension(fileName);
            String filledContent = TemplateEngine.process(value, new String[][]{{"CLASS_NAME", fNameWithoutExtension}});
            try {
                FileUtils.writeStringToFile(new File(utils.combinePaths(folder, fileName)), filledContent, Charsets.UTF_8);
            } catch (IOException ex) {
                Logger.getLogger(ScriptManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(ScriptManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addScriptToSelectedObject() {
        Basic3dObject selected = selectionManager.getCurrentSelectedObject();
        if (selected == null) {
            utils.errorMessage(mainWindow, "Please select object first");
            return;
        }
        ScriptPanel script = Wiring.getComponent(ScriptPanel.class);
        selected.addHComponent(script);
        modifyPanel.reFillPanel();
    }
}
